import os
import uuid
import script.LuaMerger as LuaMerger
import helpers.FileHelper as FileHelper
from PackageInterface import PackageInterface
from package.Feature import Feature


class Effect:

    @staticmethod
    def from_path(path):
        conf_path = os.path.join(path, "config.json")
        if os.path.exists(conf_path):
            conf = FileHelper.r_json(conf_path)
            if "name" in conf:
                return Effect(path, conf_path)
            if "tag" in conf:
                return Feature(path, conf_path)
        return Feature.from_path(path)

    def __init__(self, root_path, conf_path):
        self.root_path = root_path
        self.conf_path = conf_path

    TEST_JY = {
        "version": "0.2.0",
        "configurations": [
            {
                "type": "effect",
                "request": "launch",
                "name": "Debug readme.md",
                "program": "${workspaceFolder}/config.json"
            }
        ]
    }

    def rehash(self, prefix=""):
        conf = FileHelper.r_json(self.conf_path)
        if "name" not in conf:
            return False
        conf["name"] = prefix + uuid.uuid1().hex
        FileHelper.w_json(conf, self.conf_path)

        extra_path = os.path.join(self.root_path, "extra.json")
        extra = FileHelper.r_json(extra_path) if os.path.exists(extra_path) else {}
        extra["External_Producer"] = True
        FileHelper.w_json(extra, extra_path)

    def features(self):
        conf = FileHelper.r_json(self.conf_path)
        feature_list = conf["effect"]["Link"]
        features = []
        for feature_conf in feature_list:
            if feature_conf["type"] == "AmazingFeature":
                feature_path = os.path.join(self.root_path, self._get_feature_path(feature_conf["path"]))
                feature = Feature.from_path(feature_path)
                if feature is not None:
                    features.append(feature)
        return features

    def repair_cc_extra(self, release=True):
        conf_path = os.path.join(self.root_path, "extra.json")
        if not os.path.exists(conf_path):
            return False
        conf = FileHelper.r_json(conf_path)
        if "setting" in conf:
            setting = conf["setting"]
            print("setting")
            if "effect_adjust_params" in setting:
                print("effect_adjust_params")
                src_name = "effects_adjust_sticker" if release else "sticker"
                dst_name = "sticker" if release else "effects_adjust_sticker"
                for handler in setting["effect_adjust_params"]:
                    print(handler)
                    if handler["effect_key"] == src_name:
                        handler["effect_key"] = dst_name
                FileHelper.w_json(conf, conf_path)
                return True
        return False

    def repair_sticker_images(self):
        conf = FileHelper.r_json(self.conf_path)
        for feature_conf in conf["effect"]["Link"]:
            if feature_conf["type"] != "Sticker2DV3":
                continue
            feature = Feature.from_path(self._get_feature_path(feature_conf["path"]))
            if feature is not None:
                feature.repair_sticker_images()

    def merge_effect(self):
        return LuaMerger.merge(self.root_path, "src/EventEntry", "event")

    def merge_mv(self):
        return LuaMerger.merge(self.root_path, "src/MVEntry", "layerAnimation")

    def merge_tr(self):
        return LuaMerger.merge(self.root_path, "src/TrEntry", "event")

    def _get_feature_path(self, path):
        path = os.path.dirname(path) if path.endswith("/") else path
        return os.path.join(self.root_path, path)

